package com.ss;

import java.sql.Connection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;


public class Oscillator {

public int Oscillator(int term, String symbol)
{   Calendar cal = Calendar.getInstance();
Dbconnection db = new Dbconnection();
Connection conn=db.dbConnect("jdbc:sqlserver://localhost:1433;", "sa","");
   float stoc_val[]={};
int period;
if (term == 1)
{ 
period = 14;
}
else 
{ period = 60;
}
int k=-1;
Dbconnection db_getdata = new Dbconnection();
for (int j=0;j<period;j++)
{
DateFormat formatter ; 
formatter = new SimpleDateFormat("dd-MMM-yy");
             String s = formatter.format(cal.DATE);
String dt = s;  // Start date
try {
cal.setTime(formatter.parse(dt));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cal.add(Calendar.DATE, k);  // number of days to add
dt = formatter.format(cal.getTime());  // dt is now the new date

stoc_val[j] = db_getdata.getdata(conn, symbol, dt);
k=k-1;
}
float high;
float low;
int i;
float h[]={};
float d[]={};


high = stoc_val[0]; low = stoc_val[0];
for(i=0;i<period;i++)
{
if(high < stoc_val[i])
high = stoc_val[i];
else if(low > stoc_val[i])
low = stoc_val[i];

if(i>0)
{
h[i] = 100*((stoc_val[i]-low)/(high-low));
}

if(i>3)
{
d[i] = (h[i]+h[i-1]+h[i-2]+h[i-3])/3;
}
}


if((d[period-1]>=80) && (d[period]<80))
{
   return -1; //sell
}
if((d[period-1]<=20) && (d[period]>20))
{
return 1;
//Buy
}
else
{
return 0; //hold
}
}
}


